package com.example.gcmpractice;


import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.example.utils.NotificationFunctions;
import com.example.utils.ServerFunctions;
import com.google.android.gcm.GCMBaseIntentService;
import com.google.android.gcm.GCMConstants;

public class GCMIntentService extends GCMBaseIntentService {
	
	/**
	 * This is the unique Google API Project ID. A valid ID means that this application is
	 * allowed to use the APIs for Google Cloud Messaging.
	 * 
	 * Change this value to your API project's ID.
	 */
	private static final String API_PROJECT_ID = "195461334235";
	
	/**
	 * This is the key value the server uses when sending a collapsible message, like a
	 * message that just prompts the app to poll the server for new data.
	 * 
	 */
	private static final String COLLAPSE_KEY = "COLLAPSE";
	
	/**
	 * This constructor must be overridden as it is.
	 */
	public GCMIntentService() {
		super(API_PROJECT_ID);
	}

	@Override
	protected void onError(Context context, String errorID) {
		// There is an error in registration attempt with Google Cloud, not the application server.
		// In the app should create a notification notifying the error to the user.
		
		String errMsg = "";
		// The phone user needs to create or sign in to a Google Account.
		if (errorID.equals(GCMConstants.ERROR_ACCOUNT_MISSING)) {
			errMsg = "Push notification activation failure: Device needs Google account.";
		}
		// Google account password is incorrect.
		else if (errorID.equals(GCMConstants.ERROR_AUTHENTICATION_FAILED)) {
			errMsg = "Push notification activation failure: Incorrect Google Account Credential";
		}
		//Incorrect project API ID. Developer should fix it.
		else	if (errorID.equals(GCMConstants.ERROR_INVALID_SENDER)) {
			errMsg = "Push notification activation failure: Incorrect API ID. ";
		}
		//Phone doesn't support GCM
		else	if (errorID.equals(GCMConstants.ERROR_PHONE_REGISTRATION_ERROR)
				|| errorID.equals(GCMConstants.ERROR_INVALID_PARAMETERS)) {
			errMsg = "Push notification activation failure: Service not support for Android 2.1 and below.";
		}
		else {
			errMsg = "Push notification activation failure: Unidentifiable Error Code.";
		}
		
		PendingIntent noActIntent = NotificationFunctions.makeNotifHandlerNoAction(context);
		Notification notif = NotificationFunctions.makeNotification(context, 
				noActIntent, 
				"New Message", 
				System.currentTimeMillis(), 
				"App_name", 
				errMsg, 
				R.drawable.ic_notify);
		NotificationFunctions.broadcastNotification(0, context, notif);
		
	}

	@Override
	protected void onMessage(Context context, Intent intent) {
		// Google Cloud has relayed a message from the server to the app.
		// Handle this message. In this example, we'll just generate a notification.
		
		//If the message has a payload, we can retrieve it by the intent bundle from the parameters.
		String collapseAble = intent.getStringExtra(COLLAPSE_KEY);
		if (collapseAble == null) {
			/*
			 * This is a message where each notification message has an entry within the
			 * notification manager bar itself.
			 * 
			 * In a context such as instant messaging where the user is receiving tells from
			 * multiple other users, it is useful for the user to see the content of each different
			 * message.
			 * 
			 */
			//BasicFunctions.buildNotification(100, context, "Collapseable Message", "Pushed Notification");
			
			PendingIntent noActIntent = NotificationFunctions.makeNotifHandlerNoAction(context);
			Notification notif = NotificationFunctions.makeNotification(context, 
					noActIntent, 
					"New Message", 
					System.currentTimeMillis(), 
					"App_name", 
					"Message Content", 
					R.drawable.ic_notify);
			NotificationFunctions.broadcastNotification(0, context, notif);
		}
		else {
			/*
			 * A collapseable message is the type of message where only the latest message within
			 * the category will be displayed. The primary usage of this kind of message is to notify
			 * the application that it should take some action.
			 * 
			 * For example:
			 * 
			 * The message with category "You got mail!" is the kind of notification that needs to
			 * be displayed only once. No new notification should be displayed until the user 
			 * checks her Email. 
			*/
			PendingIntent noActIntent = NotificationFunctions.makeNotifHandler(context, MainAct.class, new Bundle());
			Notification notif = NotificationFunctions.makeNotification(context, 
					noActIntent, 
					"New Message", 
					System.currentTimeMillis(), 
					"App_name", 
					"Message Content", 
					R.drawable.ic_notify);
			NotificationFunctions.broadcastNotification(-1, context, notif);			
		}
	}

	@Override
	protected void onRegistered(Context context, String rID) {
		//This is the event from GCM stating that the device has be registered on the cloud.
		//The application should register with the third party server.
		ServerFunctions.registerWithServer(context, rID);
	}
	
	@Override
	protected void onUnregistered(Context context, String rID) {
		//The application has unregistered from the cloud.
		//The application should also unregister from the third party server.
		ServerFunctions.unregisterWithServer(context, rID);
	}

}
